﻿<%@ Page Title="" Language="C#" MasterPageFile="~/General.Master" AutoEventWireup="true" CodeBehind="Home_ChangePassword.aspx.cs" Inherits="NCAB.WF.Home_ChangePassword" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

    <title>NetCorp | Change Password</title>
    <link href="css/plugins/sweetalert/sweetalert.css" rel="stylesheet" />



</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-lg-10">
            <h2>Profile</h2>
            <ol class="breadcrumb">
                <li>
                    <a href="Home.aspx">Home</a>
                </li>
                <li class="active">
                    <strong>Change Password</strong>
                </li>
            </ol>
        </div>
        <div class="col-lg-2">
        </div>
    </div>

    <div class="wrapper wrapper-content">
        <div class="row animated fadeInRight">
            <div class="col-md-8">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>Change Password</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                        </div>
                    </div>
                    <div>
                        <div class="ibox-content">
                            <div class="form-horizontal">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">Old Password</label>
                                    <div class="col-sm-8">
                                        <asp:TextBox ID="txtOldPassword" type="password" class="form-control" runat="server" placeholder="Old Password" required></asp:TextBox>
                                    </div>
                                </div>
                                <div class="hr-line-dashed"></div>
                                <div id="div_NewPassword"  class="form-group">
                                    <label class="col-sm-4 control-label">New Password</label>
                                    <div class="col-sm-8">
                                        <asp:TextBox ID="txtNewPassword" type="password" class="form-control" runat="server" placeholder="New Password" required></asp:TextBox>
                                    </div>
                                </div>
                                <div class="hr-line-dashed"></div>
                                <div id="div_ConfirmPassword" class="form-group">
                                    <label class="col-sm-4 control-label">Confirm New Password</label>
                                    <div class="col-sm-8">
                                        <asp:TextBox ID="txtConfirmPassword" type="password" runat="server" class="form-control" placeholder="Confirm Password" required></asp:TextBox>
                                    </div>
                                </div>
                                <div class="hr-line-dashed"></div>
                                <div class="form-group">
                                    <div class="col-sm-4 col-sm-offset-4">
                                        <asp:Button ID="btnChangePassword" class="btn btn-primary" UseSubmitBehavior="true" type="submit" runat="server" Text="Save Changes"></asp:Button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="js/plugins/sweetalert/sweetalert.min.js"></script>

    <script>
        $("#form1").submit(function (e) {

            var old_password = $('[id$=txtOldPassword]').val();
            var new_password = $('[id$=txtNewPassword]').val();
            var confirm = $('[id$=txtConfirmPassword]').val();

            if (new_password == confirm) {
                $('[id$=div_NewPassword]').removeClass("has-error");
                $('[id$=div_ConfirmPassword]').removeClass("has-error");

                swal({
                    title: "¿Estás seguro?",
                    text: "Con esta acción cambiará su password de inicio de sesión",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "OK!",
                    cancelButtonText: "Cancel!",
                    closeOnConfirm: false,
                    closeOnCancel: false
                },
                    function (isConfirm) {
                        if (isConfirm) {
                            $.ajax({
                                crossDomain: true,
                                type: "POST",
                                url: "services/WS_User.asmx/UpdatePassword",
                                data: "{'OldPassword':'" + old_password + "', 'NewPassword':'" + new_password + "'}",
                                contentType: "application/json; charset=UTF-8",
                                dataType: "json",
                                success: function (data) {

                                    var resultado = $.parseJSON(data.d);
                                    
                                    if (resultado == true) {
                                        swal("¡OK!", "Su password cambio correctamente.", "success");
                                        $("#form1").find("input[type=password]").val("");
                                    }
                                    else {
                                        swal("Cancelado", "Verifique Información", "error");
                                        $('[id$=txtOldPassword]').focus();
                                        toastr.options = { closeButton: true, progressBar: true, showMethod: 'slideDown', timeOut: 4000 };
                                        toastr.error("No se pudo cambiar el password. Revise los datos suministrados.", "Error");
                                    }
                                },
                                error: function (xhr, ajaxOptions, thrownError) {
                                    location.href = "Error.aspx";
                                }
                            });
                        }
                        else {
                            swal("Cancelado", "Canceló la acción", "error");
                        }
                    });
            }
            else {
                $('[id$=div_NewPassword]').addClass("has-error");
                $('[id$=div_ConfirmPassword]').addClass("has-error");
                $('[id$=txtNewPassword]').focus();

                toastr.options = { closeButton: true, progressBar: true, showMethod: 'slideDown', timeOut: 4000 };
                toastr.error("El nuevo password y la confirmación no son idénticos.", "Error");
            }
            e.preventDefault();
        });

    </script>

</asp:Content>
